local yeshi_mashu = fk.CreateSkill {
  name = "th_yeshi&",
}

yeshi_mashu:addEffect("active", {
  mute = true,
  max_phase_use_time = 1,
  can_use = function(self, player)
    return table.find(Fk:currentRoom().alive_players, function(p) return p:hasSkill("th_yeshi") and p ~= player and Fk.skill_skels["th_yeshi"]:withinTimesLimit(p, Player.HistoryPhase) end)
  end,
  prompt = function(self, player, cards, targets)
    return targets[1] and "#th_yeshi&:" .. targets[1].id or "#th_yeshi&0"
  end,
  card_num = 1,
  card_filter = function(self, player, to_select, selected)
    return #selected == 0
  end,
  target_num = function(self, player)
    if #table.filter(Fk:currentRoom().alive_players, function(p)
      return p:hasSkill("th_yeshi") and p ~= player and Fk.skill_skels["th_yeshi"]:withinTimesLimit(p, Player.HistoryPhase)
    end) > 1 then
      return 1
    else
      return 0
    end
  end,
  target_filter = function(self, player, to_select, selected)
    if #table.filter(Fk:currentRoom().alive_players, function(p)
      return p:hasSkill("th_yeshi") and p ~= player and Fk.skill_skels["th_yeshi"]:withinTimesLimit(p, Player.HistoryPhase)
    end) > 1 then
      return #selected == 0 and to_select ~= player and to_select:hasSkill("th_yeshi") and Fk.skill_skels["th_yeshi"]:withinTimesLimit(to_select, Player.HistoryPhase)
    else
      return false
    end
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local targets = effect.tos[1] and effect.tos or table.filter(room.alive_players, function(p)
      return p:hasSkill("th_yeshi") and p ~= player and Fk.skill_skels["th_yeshi"]:withinTimesLimit(p, Player.HistoryPhase)
    end)
    local target
    if #targets == 1 then
      target = targets[1]
    else
      target = room:askToChoosePlayers(player, {
        min_num = 1,
        max_num = 1,
        targets = targets,
        skill_name = "th_yeshi",
        prompt = "#th_yeshi&0",
        cancelable = false,
      })[1]
    end
    local card = Fk:getCardById(effect.cards[1])
    room:moveCardTo(card, Player.Hand, target, fk.ReasonGive, yeshi_mashu.name, nil, false)
    local choice_draw = "#th_yeshi-draw::"..player.id
    local choice_mashu = "#th_yeshi-mashu::"..player.id
    local choice = room:askToChoice(target, {
      choices = {choice_draw, choice_mashu},
      skill_name = "th_yeshi",
      cancelable = false
    })
    target:addSkillUseHistory("th_yeshi")
    if choice == choice_mashu then
      room:notifySkillInvoked(target, "th_yeshi", "support")
      room:addPlayerMark(player, "@@th_yeshi-turn", 1)
    else
      room:notifySkillInvoked(target, "th_yeshi", "drawcard")
      target:drawCards(1, "th_yeshi")
      player:drawCards(1, "th_yeshi")
    end
  end,
})

Fk:loadTranslationTable{
  ["th_yeshi&"] = "夜市",
  [":th_yeshi&"] = "出牌阶段限一次，你可以交给米斯蒂娅一张牌，然后其与你摸一张牌或令你本回合与其他角色的距离-1。",

  ["#th_yeshi&"] = "你可以交给%src一张牌，然后其与你摸一张牌或令你本回合与其他角色的距离-1",
  ["#th_yeshi&0"] = "你可以交给米斯蒂娅一张牌，然后其与你摸一张牌或令你本回合与其他角色的距离-1",
}

return yeshi_mashu